package org.cocos2dx.javascript;
import android.support.annotation.MainThread;
import android.util.Log;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.Toast;
import android.view.WindowManager;
import android.util.DisplayMetrics;

import com.bytedance.sdk.openadsdk.AdSlot;
import com.bytedance.sdk.openadsdk.FilterWord;
import com.bytedance.sdk.openadsdk.TTAdConfig;
import com.bytedance.sdk.openadsdk.TTAdConstant;
import com.bytedance.sdk.openadsdk.TTAdDislike;
import com.bytedance.sdk.openadsdk.TTAdManager;
import com.bytedance.sdk.openadsdk.TTAdNative;
import com.bytedance.sdk.openadsdk.TTAdSdk;
import com.bytedance.sdk.openadsdk.TTAppDownloadListener;
import com.bytedance.sdk.openadsdk.TTBannerAd;
import com.bytedance.sdk.openadsdk.TTFullScreenVideoAd;
import com.bytedance.sdk.openadsdk.TTNativeExpressAd;
import com.bytedance.sdk.openadsdk.TTRewardVideoAd;

import org.cocos2dx.lib.Cocos2dxJavascriptJavaBridge;

import java.util.List;

public class WDNativePlatform {
    private static WDNativePlatform instance;
    public static WDNativePlatform getInstance(){
        if(instance==null)
        {
            instance = new WDNativePlatform();
        }
        return instance;
    };
    public static AppActivity app = null;
    private static boolean mHasShowDownloadActive = false;
    private static TTRewardVideoAd mttRewardVideoAd;
    private static TTAdNative mTTAdNative;
    private static int mAdCompleted = 0;
    private static String mParam;
    private static String mUserName;
    private static TTNativeExpressAd mTTAd;             //>Banner广告
    private static TTNativeExpressAd mTTExpressAd;      //>信息流广告
    private static TTNativeExpressAd mTTExpressAdBanner;//>信息流Banner广告
    static boolean adLoadError = false;
    static boolean fullScreenAdLoadError = false;
    public static LinearLayout mBannerContainer;
    public static FrameLayout mExpressContainer;
    public static LinearLayout mExpressBannerContainer;

    public static float mPhoneWidth=720;
    public static float mPhoneHeight=1280;

    public static void init (AppActivity a) {
        Log.e(Config.LogTag, "初始化===========================================");

        app = a;
        TTAdSdk.init(app.getBaseContext(),new TTAdConfig.Builder()
                        .appId(Config.AD)
                        .useTextureView(false) //使用TextureView控件播放视频,默认为SurfaceView,当有SurfaceView冲突的场景，可以使用TextureView
                        .appName(Config.APP_NAME)
                        .titleBarTheme(TTAdConstant.TITLE_BAR_THEME_DARK)
                        .allowShowNotify(true) //是否允许sdk展示通知栏提示
                        .allowShowPageWhenScreenLock(true) //是否在锁屏场景支持展示广告落地页
                        .debug(false) //测试阶段打开，可以通过日志排查问题，上线时去除该调用
                        .directDownloadNetworkType(TTAdConstant.NETWORK_STATE_WIFI, TTAdConstant.NETWORK_STATE_3G) //允许直接下载的网络状态集合
                        .supportMultiProcess(false) //是否支持多进程，true支持
                        //.httpStack(new MyOkStack3())//自定义网络库，demo中给出了okhttp3版本的样例，其余请自行开发或者咨询工作人员。
                        .build());
        //step1:初始化sdk
        TTAdManager ttAdManager = TTAdSdk.getAdManager();
        //step2:(可选，强烈建议在合适的时机调用):申请部分权限，如read_phone_state,防止获取不了imei时候，下载类广告没有填充的问题。
        TTAdSdk.getAdManager().requestPermissionIfNecessary(a);
        //step3:创建TTAdNative对象,用于调用广告请求接口
        mTTAdNative = ttAdManager.createAdNative(a.getApplicationContext());

        //常规banner
        mBannerContainer=new LinearLayout(app.getApplicationContext());
        mBannerContainer.setOrientation(LinearLayout.VERTICAL);
        FrameLayout.LayoutParams params=new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT,FrameLayout.LayoutParams.WRAP_CONTENT);
        params.gravity= Gravity.BOTTOM;
        app.addContentView(mBannerContainer,params);
        mBannerContainer.setVisibility(View.INVISIBLE);
        mBannerContainer.setHorizontalGravity(Gravity.CENTER);

        //常规信息流
        mExpressContainer=new FrameLayout(app.getApplicationContext());
        FrameLayout.LayoutParams params2=new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,FrameLayout.LayoutParams.WRAP_CONTENT);
        params2.gravity= Gravity.TOP;
        app.addContentView(mExpressContainer, params2);
        mExpressContainer.setVisibility(View.INVISIBLE);

        mPhoneWidth=getPhoneWidth();
        mPhoneHeight=getPhoneHeight();

        //信息流Banner
        mExpressBannerContainer=new LinearLayout(app.getApplicationContext());
        mExpressBannerContainer.setOrientation(LinearLayout.VERTICAL);
        FrameLayout.LayoutParams params3=new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,FrameLayout.LayoutParams.WRAP_CONTENT);
        params3.gravity= Gravity.BOTTOM;
        app.addContentView(mExpressBannerContainer,params3);
        mExpressBannerContainer.setVisibility(View.INVISIBLE);
        mExpressBannerContainer.setHorizontalGravity(Gravity.CENTER);


        loadAd();
        loadBannerAd();
        loadNativeExpressAd();
        loadFullScreenAd();
    }

    public static void login(String userName, String playerFlag) {

    }


    public static void customEvent(String event) {
        //==========>customEvent
    }

    public static void onResume() {

    }

    public static void onPause() {
        
    }

    public static void showAd()
    {
        Log.d("TT","-------------------------------------------------------------------");
        if (mttRewardVideoAd != null) {
            Log.d("TT","----------------------showAd-------------");
            app.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mttRewardVideoAd.showRewardVideoAd(app);
                    mttRewardVideoAd = null;
                }
            });
        }else
        {
            callJS("error");
            //广告加载失败，继续加载，防止网络断开不出广告的情况
            adLoadError = true;
            loadAd();
        }
    }

    public static void showFullVideo(){
        //全屏视频show
        if (mttFullVideoAd != null) {
            //step6:在获取到广告后展示
            //该方法直接展示广告
            //mttFullVideoAd.showFullScreenVideoAd(FullScreenVideoActivity.this);

            //展示广告，并传入广告展示的场景
            app.runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    mttFullVideoAd.showFullScreenVideoAd(app,TTAdConstant.RitScenes.GAME_GIFT_BONUS,null);
                    mttFullVideoAd = null;
                }
            });


        } else {
            Log.d("TT", "请先加载广告");
            callJS("error");
            //广告加载失败，继续加载，防止网络断开不出广告的情况
            fullScreenAdLoadError = true;
            loadFullScreenAd();
        }
    }


    private static float mCurnativeExpressAdCenterPosY = 0;
    private static float mRespectWidth = 360;
    private static float mRespectHeight = 200;
    //显示原生信息流广告
    public static void showBannerAd(){
        app.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mShowNativeExpressAd = true;
                mBannerContainer.setVisibility(View.VISIBLE);
            }
        });
    }

    public static void hideBannerAd(){
        app.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mShowNativeExpressAd = false;
                mBannerContainer.setVisibility(View.INVISIBLE);
                loadBannerAd();
            }
        });
    }
    //显示原生信息流广告
    public static void showFeedAd(){
        app.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mShowNativeExpressAd = true;
                mExpressContainer.setVisibility(View.VISIBLE);
            }
        });
    }

    public static void hideFeedAd(){
        app.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mShowNativeExpressAd = false;
                mExpressContainer.setVisibility(View.INVISIBLE);
                loadNativeExpressAd();
            }
        });
    }

//======================原生信息流Banner广告================================
    //显示原生信息流广告
    public static void showFeedBannerAd(){
        Log.d(Config.LogTag,"显示原生信息流Banner广告: ");
        app.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mShowNativeExpressAdBanner = true;
                mExpressBannerContainer.setVisibility(View.VISIBLE);
            }
        });
    }

    public static void hideFeedBannerAd(){
        Log.d(Config.LogTag,"隐藏原生信息流Banner广告: ");

        app.runOnUiThread(new Runnable() {
            @Override
            public void run() {
                mShowNativeExpressAdBanner = false;
                mExpressBannerContainer.setVisibility(View.INVISIBLE);
                loadNativeExpressAdBanner();
            }
        });
    }


    private static void callJS (String param) {
        mParam = param;
        app.runOnGLThread(new Runnable() {
            @Override
            public void run() {
                Cocos2dxJavascriptJavaBridge.evalString("window.wdNativeVideoResult('" + mParam + "')");
            }
        });
    }

    private static void callFullScreenJS (String param) {
        mParam = param;
        app.runOnGLThread(new Runnable() {
            @Override
            public void run() {
                Cocos2dxJavascriptJavaBridge.evalString("window.wdFullScreenAdResult('" + mParam + "')");
            }
        });
    }

    private static void loadAd() {
        Log.d("TT","----------------------loadAd-------------");
        //step4:创建广告请求参数AdSlot,具体参数含义参考文档
        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId(Config.RewardId)
                .setSupportDeepLink(true)
                .setImageAcceptedSize(1080, 1920)
                .setRewardName("金币") //奖励的名称
                .setRewardAmount(1)  //奖励的数量
                .setUserID("userid")//用户id,必传参数
                .setMediaExtra("") //附加参数，可选
                .setOrientation(TTAdConstant.VERTICAL) //必填参数，期望视频的播放方向：TTAdConstant.HORIZONTAL 或 TTAdConstant.VERTICAL
                .build();
        //step5:请求广告
        mTTAdNative.loadRewardVideoAd(adSlot, new TTAdNative.RewardVideoAdListener() {
            @Override
            public void onError(int code, String message) {
                Log.d("TT","----------------------mi-------------"+message);
            }

            //视频广告加载后，视频资源缓存到本地的回调，在此回调后，播放本地视频，流畅不阻塞。
            @Override
            public void onRewardVideoCached() {
                Log.d("TT","----------------------mi-------------"+"rewardVideoAd video cached");
            }

            //视频广告的素材加载完毕，比如视频url等，在此回调后，可以播放在线视频，网络不好可能出现加载缓冲，影响体验。
            @Override
            public void onRewardVideoAdLoad(TTRewardVideoAd ad) {
                Log.d("TT","----------------------mi-------------"+"rewardVideoAd loaded");
                mttRewardVideoAd = ad;
                mttRewardVideoAd.setRewardAdInteractionListener(new TTRewardVideoAd.RewardAdInteractionListener() {

                    @Override
                    public void onAdShow() {
                        Log.d("TT","----------------------mi-------------"+"rewardVideoAd show");
                    }

                    @Override
                    public void onAdVideoBarClick() {
                        Log.d("TT","----------------------mi-------------"+"rewardVideoAd bar click");
                    }

                    @Override
                    public void onAdClose() {
                        Log.d("TT","----------------------mi-------------"+"rewardVideoAd close");
                        callJS(mAdCompleted + "");
                        mAdCompleted = 0;
                        loadAd();
                    }

                    //视频播放完成回调
                    @Override
                    public void onVideoComplete() {
                        Log.d("TT","----------------------mi-------------"+"rewardVideoAd complete");
                        mAdCompleted = 1;
                    }

                    @Override
                    public void onVideoError() {
                        Log.d("TT","----------------------mi-------------"+"rewardVideoAd error");
                    }

                    //视频播放完成后，奖励验证回调，rewardVerify：是否有效，rewardAmount：奖励梳理，rewardName：奖励名称
                    @Override
                    public void onRewardVerify(boolean rewardVerify, int rewardAmount, String rewardName) {
                        Log.d("TT", "----------------------mi-------------"+"verify:" + rewardVerify + " amount:" + rewardAmount +
                                " name:" + rewardName);
                    }

                    @Override
                    public void onSkippedVideo() {
                        Log.d("TT", "----------------------mi-------------"+"rewardVideoAd has onSkippedVideo");
                    }
                });
                mttRewardVideoAd.setDownloadListener(new TTAppDownloadListener() {
                    @Override
                    public void onIdle() {
                        mHasShowDownloadActive = false;
                    }

                    @Override
                    public void onDownloadActive(long totalBytes, long currBytes, String fileName, String appName) {
                        if (!mHasShowDownloadActive) {
                            mHasShowDownloadActive = true;
                            Log.d("TT","----------------------mi-------------"+"下载中，点击下载区域暂停");
                        }
                    }

                    @Override
                    public void onDownloadPaused(long totalBytes, long currBytes, String fileName, String appName) {
                        Log.d("TT","----------------------mi-------------"+"下载暂停，点击下载区域继续");
                    }

                    @Override
                    public void onDownloadFailed(long totalBytes, long currBytes, String fileName, String appName) {
                        Log.d("TT","----------------------mi-------------"+"下载失败，点击下载区域重新下载");
                    }

                    @Override
                    public void onDownloadFinished(long totalBytes, String fileName, String appName) {
                        Log.d("TT","----------------------mi-------------"+"下载完成，点击下载区域重新下载");
                    }

                    @Override
                    public void onInstalled(String fileName, String appName) {
                        Log.d("TT","----------------------mi-------------"+"安装完成，点击下载区域打开");
                    }
                });
                if(adLoadError)
                {
                    mttRewardVideoAd.showRewardVideoAd(app);
                    mttRewardVideoAd = null;
                    adLoadError = false;
                }
            }
        });
    }

    private static int getPhoneWidth()
    {
        WindowManager manager = app.getWindowManager();
        DisplayMetrics outMetrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(outMetrics);
        int width = outMetrics.widthPixels;
        Log.d("TT","---------------------width------------:"+width);
        return  width;
    }
    private static int getPhoneHeight()
    {
        WindowManager manager = app.getWindowManager();
        DisplayMetrics outMetrics = new DisplayMetrics();
        manager.getDefaultDisplay().getMetrics(outMetrics);
        int height = outMetrics.heightPixels;
        Log.d("TT","---------------------height------------:"+height);
        return  height;
    }
    private static void loadBannerAd() {
        //设置广告参数
        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId(Config.BANNER) //广告位id
                .setSupportDeepLink(true)
                .setAdCount(1) //请求广告数量为1到3条
                .setExpressViewAcceptedSize(mPhoneWidth,120) //期望模板广告view的size,单位dp
                .setImageAcceptedSize(640, 320)//这个参数设置即可，不影响个性化模板广告的size
                .build();
        //加载广告
        mTTAdNative.loadBannerExpressAd(adSlot, new TTAdNative.NativeExpressAdListener() {
            @Override
            public void onError(int code, String message) {
//                TToast.show(NativeExpressActivity.this, "load error : " + code + ", " + message);
                mBannerContainer.removeAllViews();
            }

            @Override
            public void onNativeExpressAdLoad(List<TTNativeExpressAd> ads) {
                if (ads == null || ads.size() == 0) {
                    return;
                }
                Log.d("TT","onNativeExpressAdLoad");
                mTTAd = ads.get(0);
                mTTAd.setSlideIntervalTime(30 * 1000);//设置轮播间隔 ms,不调用则不进行轮播展示
                bindAdListener(mTTAd);
                mTTAd.render();//调用render开始渲染广告
            }
        });
    }

    //绑定广告行为
    private static void bindAdListener(TTNativeExpressAd ad) {
        ad.setExpressInteractionListener(new TTNativeExpressAd.ExpressAdInteractionListener() {
            @Override
            public void onAdClicked(View view, int type) {
//                TToast.show(mContext, "广告被点击");
            }

            @Override
            public void onAdShow(View view, int type) {
//                TToast.show(mContext, "广告展示");
            }

            @Override
            public void onRenderFail(View view, String msg, int code) {
//                Log.e("ExpressView","render fail:"+(System.currentTimeMillis() - startTime));
                Log.d("TT", msg+" code:"+code);
            }

            @Override
            public void onRenderSuccess(View view, float width, float height) {
                //返回view的宽高 单位 dp


                //在渲染成功回调时展示广告，提升体验
                mBannerContainer.removeAllViews();
                mBannerContainer.addView(view);
            }
        });
        //dislike设置
//        bindDislike(ad, false);
        if (ad.getInteractionType() != TTAdConstant.INTERACTION_TYPE_DOWNLOAD){
            return;
        }
        //可选，下载监听设置
        ad.setDownloadListener(new TTAppDownloadListener() {
            @Override
            public void onIdle() {
//                TToast.show(BannerExpressActivity.this, "点击开始下载", Toast.LENGTH_LONG);
            }

            @Override
            public void onDownloadActive(long totalBytes, long currBytes, String fileName, String appName) {
                if (!mHasShowDownloadActive) {
                    mHasShowDownloadActive = true;
//                    TToast.show(BannerExpressActivity.this, "下载中，点击暂停", Toast.LENGTH_LONG);
                }
            }

            @Override
            public void onDownloadPaused(long totalBytes, long currBytes, String fileName, String appName) {
//                TToast.show(BannerExpressActivity.this, "下载暂停，点击继续", Toast.LENGTH_LONG);
            }

            @Override
            public void onDownloadFailed(long totalBytes, long currBytes, String fileName, String appName) {
//                TToast.show(BannerExpressActivity.this, "下载失败，点击重新下载", Toast.LENGTH_LONG);
            }

            @Override
            public void onInstalled(String fileName, String appName) {
//                TToast.show(BannerExpressActivity.this, "安装完成，点击图片打开", Toast.LENGTH_LONG);
            }

            @Override
            public void onDownloadFinished(long totalBytes, String fileName, String appName) {
//                TToast.show(BannerExpressActivity.this, "点击安装", Toast.LENGTH_LONG);
            }
        });
    }

    //全屏视频
    private static TTFullScreenVideoAd mttFullVideoAd;
    @SuppressWarnings("SameParameterValue")
    private static void loadFullScreenAd() {
        //step4:创建广告请求参数AdSlot,具体参数含义参考文档
        Log.d("TT","----------------------loadFullScreenAd-------------");
        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId(Config.FullScreenId)
                .setSupportDeepLink(true)
                .setImageAcceptedSize(1080, 1920)
                .setOrientation(TTAdConstant.VERTICAL)//必填参数，期望视频的播放方向：TTAdConstant.HORIZONTAL 或 TTAdConstant.VERTICAL
                .build();
        //step5:请求广告
        mTTAdNative.loadFullScreenVideoAd(adSlot, new TTAdNative.FullScreenVideoAdListener() {
            @Override
            public void onError(int code, String message) {
                Log.d("TT", "FullScreen error msg:" + message);
            }

            @Override
            public void onFullScreenVideoAdLoad(TTFullScreenVideoAd ad) {

                mttFullVideoAd = ad;
                mttFullVideoAd.setFullScreenVideoAdInteractionListener(new TTFullScreenVideoAd.FullScreenVideoAdInteractionListener() {

                    @Override
                    public void onAdShow() {
                        Log.d("TT", "FullVideoAd show");
                    }

                    @Override
                    public void onAdVideoBarClick() {
                        Log.d("TT", "FullVideoAd bar click");
                    }

                    @Override
                    public void onAdClose() {
                        Log.d("TT", "FullVideoAd close");
                        callFullScreenJS("1");
                        loadFullScreenAd();
                    }

                    @Override
                    public void onVideoComplete() {
                        Log.d("TT", "FullVideoAd complete");
                        //mAdCompleted = 1;
                    }

                    @Override
                    public void onSkippedVideo() {
                        Log.d("TT", "FullVideoAd skipped");

                    }

                });


                ad.setDownloadListener(new TTAppDownloadListener() {
                    @Override
                    public void onIdle() {
                        mHasShowDownloadActive = false;
                    }

                    @Override
                    public void onDownloadActive(long totalBytes, long currBytes, String fileName, String appName) {
                        Log.d("DML", "onDownloadActive==totalBytes=" + totalBytes + ",currBytes=" + currBytes + ",fileName=" + fileName + ",appName=" + appName);

                        if (!mHasShowDownloadActive) {
                            mHasShowDownloadActive = true;
                            Log.d("TT", "下载中，点击下载区域暂停");
                        }
                    }

                    @Override
                    public void onDownloadPaused(long totalBytes, long currBytes, String fileName, String appName) {
                        Log.d("DML", "onDownloadPaused===totalBytes=" + totalBytes + ",currBytes=" + currBytes + ",fileName=" + fileName + ",appName=" + appName);
                        Log.d("TT", "下载暂停，点击下载区域继续");
                    }

                    @Override
                    public void onDownloadFailed(long totalBytes, long currBytes, String fileName, String appName) {
                        Log.d("DML", "onDownloadFailed==totalBytes=" + totalBytes + ",currBytes=" + currBytes + ",fileName=" + fileName + ",appName=" + appName);
                        Log.d("TT", "下载失败，点击下载区域重新下载");
                    }

                    @Override
                    public void onDownloadFinished(long totalBytes, String fileName, String appName) {
                        Log.d("DML", "onDownloadFinished==totalBytes=" + totalBytes + ",fileName=" + fileName + ",appName=" + appName);
                        Log.d("TT", "下载完成，点击下载区域重新下载");
                    }

                    @Override
                    public void onInstalled(String fileName, String appName) {
                        Log.d("DML", "onInstalled==" + ",fileName=" + fileName + ",appName=" + appName);
                        Log.d("TT", "安装完成，点击下载区域打开");
                    }
                });

                if(fullScreenAdLoadError)
                {
                    mttFullVideoAd.showFullScreenVideoAd(app,TTAdConstant.RitScenes.GAME_GIFT_BONUS,null);
                    mttFullVideoAd = null;
                    fullScreenAdLoadError = false;
                }
            }

            @Override
            public void onFullScreenVideoCached() {
                Log.d("TT", "FullVideoAd video cached");
            }
        });
    }

    private static boolean mShowNativeExpressAd = false;
    private static TTNativeExpressAd mNativeExpressAd;
    //信息流广告
    private static void loadNativeExpressAd(){
        Log.d("TT","----------------------加载原生信息流广告-------------");

        //设置广告参数
        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId(Config.NativeExpressId) //广告位id
                .setSupportDeepLink(true)
                .setAdCount(1) //请求广告数量为1到3条
                .setExpressViewAcceptedSize(360,200) //必填：期望个性化模板广告view的size,单位dp
                .setImageAcceptedSize(1920,1080) //这个参数设置即可，不影响个性化模板广告的size
                .build();

        //加载广告
        mTTAdNative.loadNativeExpressAd(adSlot, new TTAdNative.NativeExpressAdListener() {
            @Override
            public void onError(int code, String message) {
                Log.d("TT","----------------------原生信息流广告load error : "  + code + "," +  message);
                //TToast.show(NativeExpressActivity.this, "load error : " + code + ", " + message);
                mExpressContainer.removeAllViews();
            }

            @Override
            public void onNativeExpressAdLoad(List<TTNativeExpressAd> ads) {

                if (ads == null || ads.size() == 0){
                    Log.d("TT","----------------------原生广告加载完成但是数量错误");
                    return;
                }
                Log.d("TT","----------------------原生广告加载成功开始渲染");

                mTTExpressAd = ads.get(0);
                bindExpressAdListener(mTTExpressAd);
                mTTExpressAd.render();//调用render开始渲染广告
            }
        });
    }

    //绑定广告行为
    private static void bindExpressAdListener(TTNativeExpressAd ad) {
        ad.setExpressInteractionListener(new TTNativeExpressAd.ExpressAdInteractionListener() {
            @Override
            public void onAdClicked(View view, int type) {
                //TToast.show(mContext, "广告被点击");
                Log.e("TT","111------------点击信息流广告强行关闭");
            }

            @Override
            public void onAdShow(View view, int type) {
                //TToast.show(mContext, "广告展示");
            }

            @Override
            public void onRenderFail(View view, String msg, int code) {
                //Log.e("ExpressView","render fail:"+(System.currentTimeMillis() - startTime));
                //TToast.show(mContext, msg+" code:"+code);
            }

            @Override
            public void onRenderSuccess(View view, float width, float height) {
                //返回view的宽高 单位 dp
                //TToast.show(mContext, "渲染成功");
                //在渲染成功回调时展示广告，提升体验
                mExpressContainer.removeAllViews();
                mExpressContainer.addView(view);
                //Log.e("TT","111------------宽度: " + width + "   高度" + height);

                //Log.e("TT","111------------" + mExpressContainer.getWidth());
                //Log.e("TT","111------------" + mExpressContainer.getHeight());
                //Log.e("TT","111------------屏幕高度" + getPhoneHeight());
                //mExpressContainer.setScaleX(0.5f);
                //mExpressContainer.setScaleY(0.5f);
                //mExpressContainer.setY(100);
                if(mCurnativeExpressAdCenterPosY <= 0){
                    mExpressContainer.setY(mPhoneHeight- (mPhoneWidth / width)*height);
                }else{
                    mExpressContainer.setY(mCurnativeExpressAdCenterPosY - (mPhoneWidth / width)*height/2);
                }

                if(!mShowNativeExpressAd){
                    mExpressContainer.setVisibility(View.INVISIBLE);
                }else{
                    mExpressContainer.setVisibility(View.VISIBLE);
                }

            }
        });

        if (ad.getInteractionType() != TTAdConstant.INTERACTION_TYPE_DOWNLOAD){
            return;
        }

    }





    private static boolean mShowNativeExpressAdBanner = false;
    private static TTNativeExpressAd mNativeExpressAdBanner;
    //信息流广告
    private static void loadNativeExpressAdBanner(){
        Log.d(Config.LogTag,"----------------------加载原生信息流Banner广告-------------");

        //设置广告参数
        AdSlot adSlot = new AdSlot.Builder()
                .setCodeId(Config.NativeExpressBannerId) //广告位id
                .setSupportDeepLink(true)
                .setAdCount(1) //请求广告数量为1到3条
                .setExpressViewAcceptedSize(360,200) //必填：期望个性化模板广告view的size,单位dp
                .setImageAcceptedSize(1920,1080) //这个参数设置即可，不影响个性化模板广告的size
                .build();

        //加载广告
        mTTAdNative.loadNativeExpressAd(adSlot, new TTAdNative.NativeExpressAdListener() {
            @Override
            public void onError(int code, String message) {
                Log.d(Config.LogTag,"----------------------原生信息流Banner广告load error : "  + code + "," +  message);
                //TToast.show(NativeExpressActivity.this, "load error : " + code + ", " + message);
                mExpressBannerContainer.removeAllViews();
            }

            @Override
            public void onNativeExpressAdLoad(List<TTNativeExpressAd> ads) {

                if (ads == null || ads.size() == 0){
                    Log.d(Config.LogTag,"----------------------原生广告加载完成但是数量错误");
                    return;
                }
                Log.d(Config.LogTag,"----------------------原生广告加载成功开始渲染");

                mTTExpressAdBanner = ads.get(0);
                bindExpressAdListener2(mTTExpressAdBanner);
                mTTExpressAdBanner.render();//调用render开始渲染广告
            }
        });
    }

    //绑定广告行为
    private static void bindExpressAdListener2(TTNativeExpressAd ad) {
        ad.setExpressInteractionListener(new TTNativeExpressAd.ExpressAdInteractionListener() {
            @Override
            public void onAdClicked(View view, int type) {
                //TToast.show(mContext, "广告被点击");
                Log.e(Config.LogTag,"111------------点击信息流广告强行关闭");
            }

            @Override
            public void onAdShow(View view, int type) {
                //TToast.show(mContext, "广告展示");
            }

            @Override
            public void onRenderFail(View view, String msg, int code) {
                //Log.e("ExpressView","render fail:"+(System.currentTimeMillis() - startTime));
                //TToast.show(mContext, msg+" code:"+code);
            }

            @Override
            public void onRenderSuccess(View view, float width, float height) {
                //返回view的宽高 单位 dp
                //TToast.show(mContext, "渲染成功");
                //在渲染成功回调时展示广告，提升体验
                mExpressBannerContainer.removeAllViews();
                mExpressBannerContainer.addView(view);


                if(!mShowNativeExpressAdBanner){
                    mExpressBannerContainer.setVisibility(View.INVISIBLE);
                }else{
                    mExpressBannerContainer.setVisibility(View.VISIBLE);
                }

            }
        });

        if (ad.getInteractionType() != TTAdConstant.INTERACTION_TYPE_DOWNLOAD){
            return;
        }

    }
    public static void register(){

    }

    public static void loginSuccess(String jsStr){
      
    }
}
